package com.cl.short_video_java.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cl.short_video_java.common.Const;
import com.cl.short_video_java.pojo.VideoUser;
import com.cl.short_video_java.pojo.VideoUserBuy;
import com.cl.short_video_java.pojo.VideoVideo;
import com.cl.short_video_java.service.VideoUserBuyService;
import com.cl.short_video_java.mapper.VideoUserBuyMapper;
import com.cl.short_video_java.service.VideoUserService;
import com.cl.short_video_java.service.VideoVideoService;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * @author object
 * @description 针对表【video_user_buy】的数据库操作Service实现
 * @createDate 2024-10-31 15:58:59
 */
@Service
public class VideoUserBuyServiceImpl extends ServiceImpl<VideoUserBuyMapper, VideoUserBuy>
		implements VideoUserBuyService {

	private VideoUserService videoUserService;
	private VideoVideoService videoService;

	VideoUserBuyServiceImpl(VideoUserService videoUserService, VideoVideoService videoVideoService) {
		this.videoUserService = videoUserService;
		this.videoService = videoVideoService;
	}

	/**
	 * 获取用户购买记录
	 *
	 * @param pageNum
	 * @param request
	 * @return
	 */
	@Override
	public Page<VideoUserBuy> getRecording(Integer pageNum, HttpServletRequest request) {
		VideoUser user = videoUserService.getCurrentUser(request, Const.GRADE_USER);
		Page<VideoUserBuy> page = this.page(new Page<>(pageNum, 20), new QueryWrapper<VideoUserBuy>()
				.eq("user_id", user.getId())
				.eq("is_delete", 0)
				.orderByDesc("create_time")
		);
		for (VideoUserBuy item : page.getRecords()) {
			VideoVideo video = videoService.getOne(new QueryWrapper<VideoVideo>()
					.select("title", "tags", "gold", "type")
					.eq("id", item.getVideoId()));
			item.setVideo(video);
		}
		return page;
	}
}




